package com.one.common.core.domain;

import com.one.common.core.enums.ResultCode;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class R<T>{
    int code;         //根据我们的需求前后端，约定状态码指定对应信息 如 0 1 2 3

    String msg;       //对状态码的描述

    T  data;         //其它接口返回的数据   可能是各种类型  所以使用泛型

    public static <T>R<T> ok(){
        return assembleResult(null,ResultCode.SUCCESS);
    }

    public static <T>R<T> ok(T data){
        return assembleResult(data,ResultCode.SUCCESS);
    }

    public static <T>R<T> fail(){
        return assembleResult(null,ResultCode.FAILED);
    }

    public static <T>R<T> fail(int code,String msg){
        return assembleResult(code,msg,null);
    }

    /**
     *  封装一下返回接口的实现，减小冗余，只提供OK，fail接口
     * @param resultCode
     * @param <T>
     * @return
     */
    public static <T>R<T> fail(ResultCode resultCode){
        return assembleResult(null,resultCode);
    }

    private static <T>R<T> assembleResult(int code,String msg,T data){
        R<T> r = new R<>();
        r.setCode(code);
        r.setMsg(msg);
        r.setData(data);
        return r;
    }

    private static <T>R<T> assembleResult(T data,ResultCode resultCode){
        R<T> r = new R<>();
        r.setCode(resultCode.getCode());
        r.setMsg(resultCode.getMsg());
        r.setData(data);
        return r;
    }
}